package org.wlldTest.lesson5;


import org.wlld.entity.ThreeChannelMatrix;
import org.wlld.matrixTools.Matrix;
import org.wlld.tools.FastPictureExcerpt;
import org.wlld.tools.Picture;

public class Test {
    //线性模型案例实战 1：图像摘要值 md5摘要值
    public static void main(String[] args) throws Exception {
//        String aName = creatImageName("D:\\lesson/a.jpg", 5, 20);
//        String bName = creatImageName("D:\\lesson/t.png", 5, 20);
        Picture picture = new Picture();
        ThreeChannelMatrix pic1 = picture.getThreeMatrix("D:\\lesson/a.jpg");
        ThreeChannelMatrix pic2 = picture.getThreeMatrix("D:\\lesson/b.jpg");

        FastPictureExcerpt fastPictureExcerpt = new FastPictureExcerpt();
        String aName = fastPictureExcerpt.creatImageName(pic1, 5, 20);
        String bName = fastPictureExcerpt.creatImageName(pic2, 5, 20);

        System.out.println("aName:" + aName);
        System.out.println("bName:" + bName);
    }

    public static String creatImageName(String url, int boxSize, double regionSize) throws Exception {
        Picture picture = new Picture();
        ThreeChannelMatrix pic = picture.getThreeMatrix(url);
        int x = pic.getX();
        int y = pic.getY();
        int rx = x / boxSize;
        int ry = y / boxSize;
        int cpx = x % boxSize / 2;
        int cpy = y % boxSize / 2;
        ThreeChannelMatrix pic2 = pic.cutChannel(cpx, cpy, rx * boxSize, ry * boxSize);
        StringBuilder name = new StringBuilder();
        for (int i = 0; i <= rx * (boxSize - 1); i += rx) {
            for (int j = 0; j <= ry * (boxSize - 1); j += ry) {
                Matrix h = pic2.cutChannel(i, j, rx, ry).getH();//切割一份的图像
                name.append(getName(h, regionSize));
            }
        }
        return name.toString();
    }

    public static String getName(Matrix h, double regionSize) throws Exception {
        int size = 5;
        int x = h.getX();
        int y = h.getY();
        int allRow = x / size;
        int allCol = y / size;
        MyLine myLine = new MyLine(allRow * allCol);
        for (int i = 0; i <= x - size; i += size) {
            int row = i + size / 2 + 1;
            for (int j = 0; j <= y - size; j += size) {
                int col = j + size / 2 + 1;
                double value = h.getNumber(row, col);
                double myRow = (double) row / size / allRow;//归入0-1
                double myCol = (double) col / size / allCol;//归入0-1
                myLine.insertXY(myRow, myCol, value);//插入数值
            }
        }
        myLine.start();//进行线性回归
        double myCos = myLine.getCos();//当前的余弦值
        double region = 1 / regionSize;
        double minDis = -10;
        int index = 0;//index区域
        for (int i = 0; i < regionSize; i++) {
            double du = i * region * Math.PI;
            double cos = Math.cos(du);//当前遍历区域的余弦值
            double dis = Math.abs(myCos - cos);
            if (minDis < -5 || dis < minDis) {
                minDis = dis;
                index = i;
            }
        }
        return getID(index);
    }

    public static String getID(int index) {
        if (index < 10) {
            return "0" + index;
        } else {
            return String.valueOf(index);
        }
    }
}
